package com.yicong.ants.ui.video;

import android.graphics.Color;
import android.os.Build;
import android.view.KeyEvent;
import android.view.View;

import com.cchao.simplelib.ui.activity.BaseStatefulActivity;
import com.yicong.ants.R;
import com.yicong.ants.bean.ad.AntsAd;
import com.yicong.ants.databinding.RewardVideoActivityBinding;
import com.yicong.ants.manager.AppHelper;
import com.yicong.ants.manager.RouterHelper;
import com.yicong.ants.manager.ad.AntsAdHelper;
import com.yicong.ants.view.VideoPlayerView;

import cn.jzvd.Jzvd;

public class RewardVideoActivity extends BaseStatefulActivity<RewardVideoActivityBinding> implements View.OnClickListener {
    AntsAd mAdInfo;

    @Override
    protected int getLayout() {
        return R.layout.reward_video_activity;
    }

    @Override
    protected void initEventAndData() {
        mDataBind.setClick(this);
        mDataBind.adViewTimer.startTime(mDisposable);
        mAdInfo = AntsAdHelper.getAdInfo(AntsAdHelper.Position.All);
        startVideoPlayer(mAdInfo.getDisplay());
    }

    @Override
    protected void onLoadData() {
    }

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return keyCode == KeyEvent.KEYCODE_BACK;
    }

    /**
     * 开始播放视频
     */
    private void startVideoPlayer(String videoUrl) {
        VideoPlayerView player = mDataBind.videoPlayer;
        String vUrl = AppHelper.getProxy().getProxyUrl(videoUrl);
        player.setUp(vUrl, "");
        player.startVideo();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Jzvd.releaseAllVideos();
    }

    /**
     * 全屏处理
     */
    public void setStatusBarTransparent() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().setStatusBarColor(Color.TRANSPARENT);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(0x44000000);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.click_target:
                AntsAdHelper.adClicked(mAdInfo.getAd_id());
                String url = mAdInfo.getUrl();
                // 能被路由
                RouterHelper.router(thisActivity, url);
                break;
        }
    }
}
